﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using hsgsSHXX.DBUtility;//Please add references
using System.Collections.Generic;
namespace hsgsSHXX.DAL
{
	/// <summary>
	/// 数据访问类:hxcjy
	/// </summary>
	public partial class hxcjy
	{
		public hxcjy()
		{}
		#region  Method



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(hsgsSHXX.Model.hxcjy model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into hxcjy(");
			strSql.Append("ID,bm,xm,xb,csny,zzmm,whcd,sfzz,ygzdw,jtzz,lxdh,zrqy,bz)");
			strSql.Append(" values (");
			strSql.Append("@ID,@bm,@xm,@xb,@csny,@zzmm,@whcd,@sfzz,@ygzdw,@jtzz,@lxdh,@zrqy,@bz)");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@bm", SqlDbType.NVarChar,255),
					new SqlParameter("@xm", SqlDbType.NVarChar,255),
					new SqlParameter("@xb", SqlDbType.NVarChar,255),
					new SqlParameter("@csny", SqlDbType.NVarChar,255),
					new SqlParameter("@zzmm", SqlDbType.NVarChar,255),
					new SqlParameter("@whcd", SqlDbType.NVarChar,255),
					new SqlParameter("@sfzz", SqlDbType.NVarChar,255),
					new SqlParameter("@ygzdw", SqlDbType.NVarChar,255),
					new SqlParameter("@jtzz", SqlDbType.NVarChar,255),
					new SqlParameter("@lxdh", SqlDbType.NVarChar,255),
					new SqlParameter("@zrqy", SqlDbType.NVarChar,255),
					new SqlParameter("@bz", SqlDbType.NVarChar,255)};
			parameters[0].Value = model.ID;
			parameters[1].Value = model.bm;
			parameters[2].Value = model.xm;
			parameters[3].Value = model.xb;
			parameters[4].Value = model.csny;
			parameters[5].Value = model.zzmm;
			parameters[6].Value = model.whcd;
			parameters[7].Value = model.sfzz;
			parameters[8].Value = model.ygzdw;
			parameters[9].Value = model.jtzz;
			parameters[10].Value = model.lxdh;
			parameters[11].Value = model.zrqy;
			parameters[12].Value = model.bz;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(hsgsSHXX.Model.hxcjy model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update hxcjy set ");
			strSql.Append("ID=@ID,");
			strSql.Append("bm=@bm,");
			strSql.Append("xm=@xm,");
			strSql.Append("xb=@xb,");
			strSql.Append("csny=@csny,");
			strSql.Append("zzmm=@zzmm,");
			strSql.Append("whcd=@whcd,");
			strSql.Append("sfzz=@sfzz,");
			strSql.Append("ygzdw=@ygzdw,");
			strSql.Append("jtzz=@jtzz,");
			strSql.Append("lxdh=@lxdh,");
			strSql.Append("zrqy=@zrqy,");
			strSql.Append("bz=@bz");
			strSql.Append(" where ");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@bm", SqlDbType.NVarChar,255),
					new SqlParameter("@xm", SqlDbType.NVarChar,255),
					new SqlParameter("@xb", SqlDbType.NVarChar,255),
					new SqlParameter("@csny", SqlDbType.NVarChar,255),
					new SqlParameter("@zzmm", SqlDbType.NVarChar,255),
					new SqlParameter("@whcd", SqlDbType.NVarChar,255),
					new SqlParameter("@sfzz", SqlDbType.NVarChar,255),
					new SqlParameter("@ygzdw", SqlDbType.NVarChar,255),
					new SqlParameter("@jtzz", SqlDbType.NVarChar,255),
					new SqlParameter("@lxdh", SqlDbType.NVarChar,255),
					new SqlParameter("@zrqy", SqlDbType.NVarChar,255),
					new SqlParameter("@bz", SqlDbType.NVarChar,255)};
			parameters[0].Value = model.ID;
			parameters[1].Value = model.bm;
			parameters[2].Value = model.xm;
			parameters[3].Value = model.xb;
			parameters[4].Value = model.csny;
			parameters[5].Value = model.zzmm;
			parameters[6].Value = model.whcd;
			parameters[7].Value = model.sfzz;
			parameters[8].Value = model.ygzdw;
			parameters[9].Value = model.jtzz;
			parameters[10].Value = model.lxdh;
			parameters[11].Value = model.zrqy;
			parameters[12].Value = model.bz;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete()
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from hxcjy ");
			strSql.Append(" where ");
			SqlParameter[] parameters = {
};

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hsgsSHXX.Model.hxcjy GetModel()
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,bm,xm,xb,csny,zzmm,whcd,sfzz,ygzdw,jtzz,lxdh,zrqy,bz from hxcjy ");
			strSql.Append(" where ");
			SqlParameter[] parameters = {
};

			hsgsSHXX.Model.hxcjy model=new hsgsSHXX.Model.hxcjy();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["bm"]!=null && ds.Tables[0].Rows[0]["bm"].ToString()!="")
				{
					model.bm=ds.Tables[0].Rows[0]["bm"].ToString();
				}
				if(ds.Tables[0].Rows[0]["xm"]!=null && ds.Tables[0].Rows[0]["xm"].ToString()!="")
				{
					model.xm=ds.Tables[0].Rows[0]["xm"].ToString();
				}
				if(ds.Tables[0].Rows[0]["xb"]!=null && ds.Tables[0].Rows[0]["xb"].ToString()!="")
				{
					model.xb=ds.Tables[0].Rows[0]["xb"].ToString();
				}
				if(ds.Tables[0].Rows[0]["csny"]!=null && ds.Tables[0].Rows[0]["csny"].ToString()!="")
				{
					model.csny=ds.Tables[0].Rows[0]["csny"].ToString();
				}
				if(ds.Tables[0].Rows[0]["zzmm"]!=null && ds.Tables[0].Rows[0]["zzmm"].ToString()!="")
				{
					model.zzmm=ds.Tables[0].Rows[0]["zzmm"].ToString();
				}
				if(ds.Tables[0].Rows[0]["whcd"]!=null && ds.Tables[0].Rows[0]["whcd"].ToString()!="")
				{
					model.whcd=ds.Tables[0].Rows[0]["whcd"].ToString();
				}
				if(ds.Tables[0].Rows[0]["sfzz"]!=null && ds.Tables[0].Rows[0]["sfzz"].ToString()!="")
				{
					model.sfzz=ds.Tables[0].Rows[0]["sfzz"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ygzdw"]!=null && ds.Tables[0].Rows[0]["ygzdw"].ToString()!="")
				{
					model.ygzdw=ds.Tables[0].Rows[0]["ygzdw"].ToString();
				}
				if(ds.Tables[0].Rows[0]["jtzz"]!=null && ds.Tables[0].Rows[0]["jtzz"].ToString()!="")
				{
					model.jtzz=ds.Tables[0].Rows[0]["jtzz"].ToString();
				}
				if(ds.Tables[0].Rows[0]["lxdh"]!=null && ds.Tables[0].Rows[0]["lxdh"].ToString()!="")
				{
					model.lxdh=ds.Tables[0].Rows[0]["lxdh"].ToString();
				}
				if(ds.Tables[0].Rows[0]["zrqy"]!=null && ds.Tables[0].Rows[0]["zrqy"].ToString()!="")
				{
					model.zrqy=ds.Tables[0].Rows[0]["zrqy"].ToString();
				}
				if(ds.Tables[0].Rows[0]["bz"]!=null && ds.Tables[0].Rows[0]["bz"].ToString()!="")
				{
					model.bz=ds.Tables[0].Rows[0]["bz"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,bm,xm,xb,csny,zzmm,whcd,sfzz,ygzdw,jtzz,lxdh,zrqy,bz ");
			strSql.Append(" FROM hxcjy ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,bm,xm,xb,csny,zzmm,whcd,sfzz,ygzdw,jtzz,lxdh,zrqy,bz ");
			strSql.Append(" FROM hxcjy ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM hxcjy ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T. desc");
			}
			strSql.Append(")AS Row, T.*  from hxcjy T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "hxcjy";
			parameters[1].Value = "";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
        public List<hsgsSHXX.Model.hxcjy> DataTableToList(DataTable dt)
        {
            List<hsgsSHXX.Model.hxcjy> modelList = new List<hsgsSHXX.Model.hxcjy>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                hsgsSHXX.Model.hxcjy model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new hsgsSHXX.Model.hxcjy();
                    if (dt.Rows[n]["ID"] != null && dt.Rows[n]["ID"].ToString() != "")
                    {
                        model.ID = int.Parse(dt.Rows[n]["ID"].ToString());
                    }
                    if (dt.Rows[n]["bm"] != null && dt.Rows[n]["bm"].ToString() != "")
                    {
                        model.bm = dt.Rows[n]["bm"].ToString();
                    }
                    if (dt.Rows[n]["xm"] != null && dt.Rows[n]["xm"].ToString() != "")
                    {
                        model.xm = dt.Rows[n]["xm"].ToString();
                    }
                    if (dt.Rows[n]["xb"] != null && dt.Rows[n]["xb"].ToString() != "")
                    {
                        model.xb = dt.Rows[n]["xb"].ToString();
                    }
                    if (dt.Rows[n]["csny"] != null && dt.Rows[n]["csny"].ToString() != "")
                    {
                        model.csny = dt.Rows[n]["csny"].ToString();
                    }
                    if (dt.Rows[n]["zzmm"] != null && dt.Rows[n]["zzmm"].ToString() != "")
                    {
                        model.zzmm = dt.Rows[n]["zzmm"].ToString();
                    }
                    if (dt.Rows[n]["whcd"] != null && dt.Rows[n]["whcd"].ToString() != "")
                    {
                        model.whcd = dt.Rows[n]["whcd"].ToString();
                    }
                    if (dt.Rows[n]["sfzz"] != null && dt.Rows[n]["sfzz"].ToString() != "")
                    {
                        model.sfzz = dt.Rows[n]["sfzz"].ToString();
                    }
                    if (dt.Rows[n]["ygzdw"] != null && dt.Rows[n]["ygzdw"].ToString() != "")
                    {
                        model.ygzdw = dt.Rows[n]["ygzdw"].ToString();
                    }
                    if (dt.Rows[n]["jtzz"] != null && dt.Rows[n]["jtzz"].ToString() != "")
                    {
                        model.jtzz = dt.Rows[n]["jtzz"].ToString();
                    }
                    if (dt.Rows[n]["lxdh"] != null && dt.Rows[n]["lxdh"].ToString() != "")
                    {
                        model.lxdh = dt.Rows[n]["lxdh"].ToString();
                    }
                    if (dt.Rows[n]["zrqy"] != null && dt.Rows[n]["zrqy"].ToString() != "")
                    {
                        model.zrqy = dt.Rows[n]["zrqy"].ToString();
                    }
                    if (dt.Rows[n]["bz"] != null && dt.Rows[n]["bz"].ToString() != "")
                    {
                        model.bz = dt.Rows[n]["bz"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        public List<Model.hxcjy> GetFondListByPage(int pageSize, int pageIndex, string strWhere, string fOrderFiled, out int total)
        {
            if (fOrderFiled == "")
            {
                fOrderFiled = "ID desc";
            }
            SqlParameter[] parameters = {  
                    new SqlParameter("@tableName", SqlDbType.VarChar, 50),  
                    new SqlParameter("@fields", SqlDbType.VarChar, 2000),  
                    new SqlParameter("@orderField", SqlDbType.VarChar, 500),  
                    new SqlParameter("@sqlWhere", SqlDbType.VarChar,2000),  
                    new SqlParameter("@pageSize", SqlDbType.Int),  
                    new SqlParameter("@pageIndex", SqlDbType.Int),  
                    new SqlParameter("@TotalPage", SqlDbType.Int),
                    new SqlParameter("@TotalRecordCount", SqlDbType.Int) 
                    };

            parameters[0].Value = "hxcjy";
            parameters[1].Value = "*";
            parameters[2].Value = fOrderFiled;
            parameters[3].Value = strWhere;
            parameters[4].Value = pageSize;
            parameters[5].Value = pageIndex;
            parameters[6].Direction = ParameterDirection.Output;
            parameters[7].Direction = ParameterDirection.Output;
            DataSet ds = DbHelperSQL.RunProcedure("up_GetRecordByPage", parameters, "ds", 60 * 30);
            total = int.Parse(parameters[7].Value.ToString());
            return DataTableToList(ds.Tables[0]);

        }
        
	}
}

